package cc.ufinity.networkdiagnose.widget;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.manager.DBManager;
import cc.ufinity.networkdiagnose.manager.FTPConfig;
import cc.ufinity.networkdiagnose.util.StringUtils;
import cc.ufinity.networkdiagnose.view.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class PingConfigWidget extends GeneralTestWidget implements View.OnClickListener {

    public final static String PREFS_PING_NAME = "ping";
    public final static String PREFS_PING_KEY = "number";
    
    public final static String CFG_SERVER = "cfgServer";
    public final static String CFG_SERVER_IP = "cfgServerIP";
    public final static String CFG_SERVER_PORT = "cfgServerPort";
    public final static String CFG_SERVER_FTP = "cfgServerFTR";

    private Activity ctx;
    private Button btnSave, btSaveServer;
    private Button btnBack;
    private EditText pingNumber, etIp, etPort;
    public PingConfigWidget(Activity context) {
        super(context);
        this.ctx = context;
        inflate(context, R.layout.ping_config, this);
        btnSave = (Button) this.findViewById(R.id.btn_pingcfg_save);
        btSaveServer = (Button)findViewById(R.id.btn_servercfg_save);
        btnBack = (Button) this.findViewById(R.id.btn_pingcfg_back);
        pingNumber = (EditText) this.findViewById(R.id.pingcfgeditText);
        
        //add at 3-5
        etIp = (EditText)findViewById(R.id.cfg_server_et);
        etPort = (EditText)findViewById(R.id.cfg_server_port_et);
        
        btnSave.setOnClickListener(this);
        btnBack.setOnClickListener(this);
        btSaveServer.setOnClickListener(this);
        
        SharedPreferences settings = ctx.getSharedPreferences(PREFS_PING_NAME, 0);
        String Number = settings.getString(PREFS_PING_KEY, "");
        pingNumber.setText(Number);
        pingNumber.setText(Number);
        
        SharedPreferences settings1 = ctx.getSharedPreferences(CFG_SERVER, 0);
        String ip = settings1.getString(CFG_SERVER_IP, "");
        String port = settings1.getString(CFG_SERVER_PORT, "");
        etIp.setText(ip);
        etPort.setText(port);
    }

    private void save() {
        String input = pingNumber.getText().toString();
        Pattern pattern = Pattern.compile("^\\d+$");
        Matcher matcher = pattern.matcher(input);
        boolean m1 = matcher.matches();
        if (StringUtils.isEmpty(input)) {
            showMessage(ctx, "请输入网络测试项测试次数.");
        } else if(!m1){
        	showMessage(ctx, "输入测试次数不合法，请重新输入！");
        } else {
            int int_input = 0;
            try {
            	int_input = Integer.parseInt(input);
            } catch(Exception e) {
            	Log.e("PingConfigWidget", "Integer.parseInt(input)", e);
            }
        	if(int_input <= 0) {
        		showMessage(ctx, "输入测试次数不合法，请重新输入！");
        	} else {
        		SharedPreferences settings = ctx.getSharedPreferences(PREFS_PING_NAME, 1);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString(PREFS_PING_KEY, input);
                editor.commit();
                showMessage(ctx, "测试次数已保存.");
              //  this.backToConfig(ctx);
        	}
        }
    }
    
	private void saveServer() {
		
		String ip = etIp.getText().toString();
		String port = etPort.getText().toString();
		
		if (ip== null || ip.equals("") || port==null || port.equals("")) {
			showMessage(ctx, "服务器/端口号不能为空！");
			return;
		}
		
		SharedPreferences settings = ctx.getSharedPreferences(CFG_SERVER,1);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(CFG_SERVER_IP, ip);
		editor.putString(CFG_SERVER_PORT, port);
		editor.commit();
		
		DBManager db = new DBManager(ctx);
		FTPConfig config = new FTPConfig();
		config.port = 21;
		config.userName = ctx.getString(R.string.ftp_name);
		config.password = ctx.getString(R.string.ftp_pwd);
		config.address = ip;
		if(db.saveFtp(config)){
			this.showMessage(ctx, "服务器已保存.");
		}else{
			this.showMessage(ctx, "服务器配置保存出错.");
		}
		db.release();
		
		GlobalApp.FTP = ip;
		this.backToConfig(ctx);
	}

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_pingcfg_save:
            save();
            break;
        case R.id.btn_servercfg_save:
        	saveServer();
        	break;
        case R.id.btn_pingcfg_back:
            this.backToConfig(ctx);
            break;
        }
    }

    
}
